<template>
  <div ref="echart" class="echart">

  </div>
</template>

<script>
import * as echarts from 'echarts'
export default {
  name: 'MagementEchart',
  props: {
    isAxisChart: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      default() {
        return {
          Xdata: [],
          series: []
        }
      }
    }

  },
  data() {
    return {
      echart: null,
      axisOption: {
        xAxis: {
          type: 'category',
          data: [],
          axisLine: {
            lineStyle: {
              color: "#17b3a3",
            },

          },
          axisLabel: {
            internal: 0,
            color: '#333'
          }

        },
        yAxis: {
          type: "value",
          axiosLine: {
            lineStyle: {
              color: "#17b3a3"
            }
          }

        },
        legend: {
          textStyle: {
            color: "#333"
          }

        },
        grid: {
          left: "20%"

        },
        color: ["#2ec7c9", "#b6a2de"],
        //提示框
        tooltip: {
          trigger: 'axis'

        },
        series: []
      },
      normalOption: {
        tooltip: {
          trigger: "item"
        },
        color: [
          "#17b3a3",
          "#AC58FA",
          "#688A08",
          "#088A68",
          "#DF0174",
          "#3ADF00",
          "#FF8000",
          "#F7BE81",
        ],
        series: []
      }

    };
  },
  computed: {
    options() {
      return this.isAxisChart ? this.axisOption : this.normalOption
    }

  },
  mounted() {

  },
  watch: {
    chartData: {
      handler: function () {
        this.initChar()

      },
      deep: true
    }

  },
  methods: {
    initChar() {
      this.initChardata()
      //判断是否创建echart实例
      if (this.echart) {
        //创建直接实例化数据
        this.echart.setOption(this.options)
      } else {
        this.echart = echarts.init(this.$refs.echart)
        //  this.echart=echarts.init(document.querySelector('.echart'))
        this.echart.setOption(this.options)
      }

    },
    initChardata() {
      if (this.isAxisChart) {
        this.axisOption.xAxis.data = this.chartData.Xdata
        this.axisOption.series = this.chartData.series
      } else {
        this.normalOption.series = this.chartData.series
      }

    }
  },
};
</script>

<style lang="scss" scoped>
</style>